package com.hlhj.funlike_business.activity.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.mymvp.mvp.BaseFgm;
import com.hlhj.funlike_business.R;
import com.hlhj.funlike_business.activity.AddCommentyAty;
import com.hlhj.funlike_business.activity.MainActivity;
import com.hlhj.funlike_business.activity.MyCommodityDetailAty;
import com.hlhj.funlike_business.activity.adapter.FoodListAdp;
import com.hlhj.funlike_business.bean.FoodListBean;
import com.hlhj.funlike_business.presenter.FoodFragmentPresenter;
import com.hlhj.funlike_business.views.FoodFragmentView;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.container.DefaultHeader;
import com.liaoinstan.springview.widget.SpringView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class FoodFragment extends BaseFgm<FoodFragmentView, FoodFragmentPresenter> implements FoodFragmentView, FoodListAdp.getResult {

    private RecyclerView recycle_list;
    private SpringView springView;
    public static int id;
    private int page = 1,position,status;
    private FoodListAdp foodListAdp;
    private List<FoodListBean.DataBeanX.DataBean> dataBeanList;

    public static FoodFragment newInstance(int type_id) {
        FoodFragment foodFragment = new FoodFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type_id", type_id);
        foodFragment.setArguments(bundle);
        return foodFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void doBeforInit() {
        id = getArguments().getInt("type_id");
    }

    @Override
    public int getContentId() {
        return R.layout.fgm_foodlist;
    }

    @NotNull
    @Override
    public FoodFragmentView bindView() {
        return this;
    }

    @NotNull
    @Override
    public FoodFragmentPresenter createPresenter() {
        return new FoodFragmentPresenter();
    }

    @Override
    public void initView() {
        recycle_list = rootView.findViewById(R.id.recycle_list);
        springView = rootView.findViewById(R.id.spring);
    }

    @Override
    public void logicStart() {
        springView.setHeader(new DefaultHeader(getContext()));
        springView.setFooter(new DefaultFooter(getContext()));
        dataBeanList = new ArrayList<>();
        foodListAdp = new FoodListAdp(dataBeanList);
        recycle_list.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        recycle_list.setAdapter(foodListAdp);


        Log.e("zy", "当前Id:" + id);
        MainActivity.foodtype_id=id;
        getPresenter().LoadFoodList(getContext(), id, page, 10);
        foodListAdp.setListener(this);

    }

    @Override
    public void setListener() {
        springView.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getPresenter().LoadFoodList(getContext(), id, page, 10);
                springView.onFinishFreshAndLoad();
            }

            @Override
            public void onLoadmore() {
                page++;
                getPresenter().LoadFoodList(getContext(), id, page, 10);
                springView.onFinishFreshAndLoad();
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.POSTING)
    public void refresh(String msg) {
        if (msg.equals("刷新商品列表")) {
            getPresenter().LoadFoodList(getContext(), MainActivity.foodtype_id, page, 10);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }


    @Override
    public void showFoodList(List<FoodListBean.DataBeanX.DataBean> dataBeans) {
        if (page == 1) {
            dataBeanList.clear();
            dataBeanList.addAll(dataBeans);
        } else {
            dataBeanList.addAll(dataBeans);
        }
        foodListAdp.notifyDataSetChanged();
    }

    @Override
    public void showFoodStatusFood(String msg) {
        Toast.makeText(getContext(), msg.toString(), Toast.LENGTH_SHORT).show();
        if (status==0){
            dataBeanList.get(position).setStatus(1);
            foodListAdp.notifyDataSetChanged();
        }else {
            dataBeanList.get(position).setStatus(0);
            foodListAdp.notifyDataSetChanged();
        }
    }


    @Override
    public void showResult(int id, int status,int position) {
        getPresenter().LoadFoodStatus(getContext(), id, status);
        this.position = position;
        this.status = status;
    }

    @Override
    public void showListener(int id) {
        Intent intent = new Intent(getContext(), MyCommodityDetailAty.class);
        intent.putExtra("id", id);
        startActivity(intent);
    }

    @Override
    public void GoEditFood(int id) {
        Intent intent = new Intent(getContext(), AddCommentyAty.class);
        intent.putExtra("id", id);
        startActivity(intent);
    }
}
